﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using WebCore.Models.ViewModels.File;
using WebCore.Bases;
using WebCore.Models.ViewModels.Admin;
using WebCore.Models.DataManagers;
using WebCore.App_Start;
using WebCore.Models.DataEntities;

namespace WebCore.Controllers
{
    [HandleError]
    public class AdminController : BaseController
    {
        //
        // GET: /Admin/
        [Authorize(Roles = "Quản trị viên, Làm nội dung")]
        public ActionResult Index()
        {
            return View();
        }

        #region File
        public ActionResult File(string rootName)
        {
            rootName = rootName ?? ConfigurationManager.AppSettings["File_RootFolder"];
            IndexViewModel model = new IndexViewModel();
            return View(model);
        }

        public ActionResult FileManagerPlugin()
        {
            return View();
        }
        #endregion

        #region Users
        public ActionResult UserList(string key, string by, string role, string state, int? page)
        {
            UserListViewModel viewModel = new UserListViewModel(key, by, role, state, page);

            return View(viewModel);
        }

        public ActionResult UserDetail(string detailID, string backurl)
        {
            UserDetailViewModel viewModel = new UserDetailViewModel();
            viewModel.BackUrl = backurl;
            if (!String.IsNullOrEmpty(detailID))
                viewModel = new UserDetailViewModel(new Guid(detailID));
            return View(viewModel);
        }

        [HttpPost]
        public ActionResult UserDetail(UserDetailViewModel viewModel, string submitButton)
        {
            if (submitButton == "Back")
            {
                return Redirect(HttpUtility.UrlDecode(viewModel.BackUrl));
            }
            else if (submitButton == "Save")
            {
                string errorCode = string.Empty;
                if (AccountManager.UpdateUserDetail(viewModel, ref errorCode))
                {
                    viewModel = new UserDetailViewModel(viewModel.UserDetailID);
                    return View(viewModel);
                }
                else
                {
                    if (errorCode == "NotMatchPassword")
                        ModelState.AddModelError("ConfirmPassword", Resources.Front.Error_PasswordDoNotMatch);
                    string state = viewModel.UserState;
                    string roles = viewModel.Roles;
                    viewModel = new UserDetailViewModel(viewModel.UserDetailID);
                    viewModel.UserState = state;
                    viewModel.Roles = roles;
                    return View(viewModel);
                }
            }
            else
            {
                string state = viewModel.UserState;
                string roles = viewModel.Roles;
                viewModel = new UserDetailViewModel(viewModel.UserDetailID);
                viewModel.UserState = state;
                viewModel.Roles = roles;
                return View(viewModel);
            }
        }
        #endregion
    }
}
